#include "stdafx.h"
#include "RectObject.h"

CRectObject::CRectObject(b2World* world) : CBaseObject(world)
,m_bIsSolid(false)
{}

CRectObject::~CRectObject()
{}

void CRectObject::Initialize()
{}

void CRectObject::Draw()
{
	const b2Vec2* vertices = m_pShape->GetVertices();
	m_Rect = core::rect<float>(vertices[0].x, vertices[0].y, vertices[2].x, vertices[2].y);
	m_Rect += core::position2d<float>(m_pB2Body->GetPosition().x, m_pB2Body->GetPosition().y);
	if(m_bIsSolid)
	{
		CRenderVideo::GetInstance()->DrawRectangle(m_Rect, m_Color);
	}
	else
	{
		CRenderVideo::GetInstance()->DrawRectangleLine(m_Rect, m_Color);
	}
}

void CRectObject::SetRect(float ULX, float ULY, float LRX, float LRY)
{
	m_Rect = core::rect<float>(ULX, ULY, LRX, LRY);
}

void CRectObject::SetRect(core::position2df& pos, float halfWidth, float halfHeight)
{
	m_Rect = core::rect<float>(pos.X - halfWidth, pos.Y - halfHeight, pos.X + halfWidth, pos.Y + halfHeight);
}

void CRectObject::SetSolid(bool isSolid)
{
	m_bIsSolid = isSolid;
}

void CRectObject::GenB2()
{	
	m_BodyDef.position.Set(m_Rect.getCenter().X, m_Rect.getCenter().Y);
	m_pB2Body = m_pB2World->CreateBody(&m_BodyDef);	
	
	m_ShapeDef.SetAsBox(m_Rect.getWidth() / 2, m_Rect.getHeight() / 2);
	if(m_fDensity > -30000)
	{
		m_ShapeDef.density = m_fDensity;
	}
	if(m_Friction > -30000)
	{
		m_ShapeDef.friction = m_Friction;
	}
	if(m_Restitution > -30000)
	{
		m_ShapeDef.restitution = m_Restitution;
	}

	m_pShape = (b2PolygonShape* )m_pB2Body->CreateShape(&m_ShapeDef);

	if(m_fDensity > -30000)
	{
		m_pB2Body->SetMassFromShapes();
	}
}